﻿using nplJobsBLL.ProcessJobSeeker;
using nplJobsCommon.JobSeeker;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;

public partial class JobSeeker_AddNewJobSeeker : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //var email = cuwJobSeeker.FindControl("Email") as TextBox;
        //email.Text = "test@yahoo.com";
        //email.Visible = false;
    }

    /// <summary>
    /// Is user exists
    /// </summary>
    /// <param name="username"></param>
    /// <returns></returns>
    #region UserExists
    private bool UserExists(string username)
    {
        if (System.Web.Security.Membership.GetUser(username) != null) { return true; }

        return false;
    }

    #endregion

    /// <summary>
    ///  Job seeker entry related events
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    #region Job seeker entry related events
    protected void cuwJobSeeker_CreatedUser(object sender, EventArgs e)
    {

        System.Web.Security.MembershipUser newUser = System.Web.Security.Membership.GetUser(cuwJobSeeker.UserName);
        Guid newUserId = (Guid)newUser.ProviderUserKey;

        ProcessInsertJobSeeker insertJobSeeker = new ProcessInsertJobSeeker();
        JobSeeker jobSeeker = new JobSeeker();
        try
        {
            jobSeeker.JobSeekerID = newUserId;
            jobSeeker.FirstName = ((TextBox)cuwJobSeeker.CreateUserStep.ContentTemplateContainer.FindControl("txtFirstName")).Text;
            jobSeeker.LastName = ((TextBox)cuwJobSeeker.CreateUserStep.ContentTemplateContainer.FindControl("txtLastName")).Text;
            jobSeeker.DOB = System.DateTime.Now;
            jobSeeker.Email = ((TextBox)cuwJobSeeker.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).Text;
            jobSeeker.NationalityID = -1;
            jobSeeker.PostalAddress = "";
            jobSeeker.GenderID = 1;
            jobSeeker.MobilePhone = "";
            jobSeeker.AboutMe = "";

            insertJobSeeker.JobSeeker = jobSeeker;
            insertJobSeeker.invoke();
            var LastJobSeekerID = insertJobSeeker.LastJobSeekerID;
        }
        catch (Exception exp)
        {
            throw new Exception(exp.Message.ToString());
        }
    }
    protected void cuwJobSeeker_CreateUserError(object sender, CreateUserErrorEventArgs e)
    {
        Label lblPasswordMsg = cuwJobSeeker.CreateUserStep.ContentTemplateContainer.FindControl("lblPasswordMsg") as Label;
        Label lblUserNameMsg = cuwJobSeeker.CreateUserStep.ContentTemplateContainer.FindControl("lblUserMsg") as Label;
        CreateUserWizard wizard = sender as CreateUserWizard;

        if (e.CreateUserError == System.Web.Security.MembershipCreateStatus.InvalidPassword)
        {
            lblPasswordMsg.Text = "Password is not secure enough";
        }
        else
        {
            lblPasswordMsg.Text = String.Empty;
        }
        if (e.CreateUserError == System.Web.Security.MembershipCreateStatus.DuplicateUserName)
        {
            lblUserNameMsg.Text = "User exists";
        }
        else
        {
            lblUserNameMsg.Text = String.Empty;
        }
    }
    protected void cuwJobSeeker_SendingMail(object sender, MailMessageEventArgs e)
    {
        var smtpAddress = WebConfigurationManager.AppSettings["SMTP_ADDRESS"];
        var smtPort = WebConfigurationManager.AppSettings["SMTP_PORT"];

        System.Web.Security.MembershipUser newUserAccount = System.Web.Security.Membership.GetUser(cuwJobSeeker.UserName);
        Guid newUserAccountId = (Guid)newUserAccount.ProviderUserKey;
        string domainName = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;
        string confirmationPage = "/EmailConfirmation.aspx?ID=" + newUserAccountId.ToString();
        string url = domainName + confirmationPage;
        e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", url);
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
        smtp.Host = smtpAddress;
        smtp.Port = Convert.ToInt32(smtPort);
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential("emailaddress", "pass");
        smtp.EnableSsl = false;
        smtp.Send(e.Message);
        e.Cancel = true;
    } 
    #endregion
    protected void cuwJobSeeker_CreatingUser(object sender, LoginCancelEventArgs e)
    {
        cuwJobSeeker.Email = cuwJobSeeker.UserName;
    }
}
